//
//  MineSettingBodyView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/27.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class MineSettingBodyView: UIView {
    
    // 常量相关
    private let titles = ["常用信息","密码管理","登录设置","支付设置","清除缓存"]
    private let pics = ["infomation_24x24","password_24x24","login_24x24","pay_24x24","clear_24x24"]
    
    // 子布局
    private var mimebodys = [MineBodyView]()
    
    // MARK: 回调
    public var complete:((_ item:Int) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        init_view()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func init_view() -> Void {
        (0..<pics.count).forEach { (i) in
            mimebodys.append(MineBodyView())
        }
    }
    
    override func layoutSubviews() {
        if mimebodys.count == 0 {
            return
        }
        for i in 0..<pics.count {
            let body = mimebodys[i]
            body.tag = i
            body.pic = pics[i]
            body.title = titles[i]
            if i != 0 {
                let line = UIView()
                line.backgroundColor = COLOR_LINE
                line.frame = CGRect(x: MARGIN10, y: CGFloat(i) * MARGIN70, width: frame.width - MARGIN20, height: 1)
                addSubview(line)
            }
            body.frame = CGRect(x: 0, y: CGFloat(i) * MARGIN70, width: frame.width, height: MARGIN70)
            addSubview(body)
            // 添加点击事件
            body.addTarget(self, action: #selector(clickItem(ui:)), for: .touchUpInside)
        }
    }
    
    @objc func clickItem(ui:UIView) -> Void {
        if let callback = complete {
            callback(ui.tag)
        }
    }
}
